<template>
  <div class="login box">
    <img src="../../static/image/Loginbg.3377d0c.jpg" alt="">
    <div class="login">
      <div class="login-title">
        <img src="../../static/image/Logotitle.1ba5466.png" alt="">
        <p>帮助有志向的年轻人通过努力学习获得体面的工作和生活!</p>
      </div>
      <div class="login_box">
        <div class="title">
          <span @click="login_type=0" :class="login_type==0?'span:nth-of-type(1)':''">密码登录</span>
          <span @click="login_type=1" :class="login_type==1?'span:nth-of-type(2)':''">短信登录</span>
        </div>
        <!--        普通账号密码登录-->
        <div class="inp" v-if="login_type==0">
          <input v-model="username" type="text" placeholder="用户名 / 手机号码" class="user">
          <input v-model="password" type="password" name="" class="pwd" placeholder="密码">
          <!--          验证码加到这里-->
          <div id="geetest1"></div>
          <div class="rember">
            <p>
              <input type="checkbox" class="no" v-model="remember" name="a"/>
              <span>记住密码</span>
            </p>
            <p @click="change_password">忘记密码</p>
          </div>
          <button class="login_btn" @click="get_geetest_captcha">登录</button>
          <p class="go_login">没有账号
            <router-link to="/user/register">立即注册</router-link>
          </p>
        </div>
        <!--        手机账号密码登录-->
        <div class="inp" v-show="login_type==1">
          <input v-model="mobile" type="text" p laceholder="手机号码" class="user">
          <div class="sms-box">
            <input v-model="sms_code" maxlength="6" type="text" class="user" placeholder="短信验证码">
            <div class="sms-btn" @click="smsHander">{{sms_text}}</div>
          </div>
          <button class="login_btn" @click="loginHander">登录</button>

          <p class="go_login">没有账号
            <router-link to="/user/register">立即注册</router-link>
          </p>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
    export default {
        name: 'Login',
        data() {
            return {
                num: 1,
                sms_code: "",
                mobile: "",
                is_send_sms: false,
                sms_text: "获取验证码",
                login_type: 0,
                username: "",
                password: "",
                remember: false,
            }
        },

        methods: {
            loginHander() {
                // 用户密码账号登录         地址，{参数}
                if (this.login_type == 0) {
                    this.$axios.post(`${this.$settings.HOST}/user/login/`, {
                        username: this.username,
                        password: this.password,
                    }).then(response => {
                        // 使用浏览器本地存储保存token
                        if (this.remember) {
                            // 记住登录状态（永久保存）
                            sessionStorage.removeItem("user_token");
                            sessionStorage.removeItem("user_id");
                            sessionStorage.removeItem("user_name");
                            sessionStorage.removeItem("user_credit");
                            sessionStorage.removeItem("credit_to_money");
                            localStorage.user_token = response.data.token;
                            localStorage.user_id = response.data.id;
                            localStorage.user_name = response.data.username;
                            localStorage.user_credit = response.data.user_credit;
                            localStorage.credit_to_money = response.data.credit_to_money;
                        } else {
                            // 不记住登录状态（客户端关闭既清除)
                            localStorage.removeItem("user_token");
                            localStorage.removeItem("user_id");
                            localStorage.removeItem("user_name");
                            localStorage.removeItem("user_credit");
                            localStorage.removeItem("credit_to_money");
                            sessionStorage.user_token = response.data.token;
                            sessionStorage.user_id = response.data.id;
                            sessionStorage.user_name = response.data.username;
                            sessionStorage.user_credit = response.data.user_credit;
                            sessionStorage.credit = response.data.user_credit;
                            sessionStorage.credit_to_money = response.data.credit_to_money
                        }
                        // 登录成功页面跳转
                        let self = this;
                        this.$alert("登录成功！", "路飞学城", {
                            callback() {
                                // self.$router.push("/");
                                self.$router.go(-1);
                            }
                        });
                    }).catch(error => {
                        this.$message.error("对不起，登录失败！请确认密码或账号是否正确！");
                    });

                } else { // 用户手机号和验证码登录
                    this.$axios.post(`${this.$settings.HOST}/user/login2/`, {
                        // post 提交用户和验证码信息
                        username: this.username,
                        sms_code: this.sms_code,
                    }).then(response => {
                        // 使用浏览器本地存储保存token
                        if (this.remember) {
                            // 记住登录状态（永久保存）
                            sessionStorage.removeItem("user_token");
                            sessionStorage.removeItem("user_id");
                            sessionStorage.removeItem("user_name");
                            localStorage.user_token = response.data.token;
                            localStorage.user_id = response.data.id;
                            localStorage.user_name = response.data.username;

                        } else {
                            // 不记住登录状态（客户端关闭既清除)
                            localStorage.removeItem("user_token");
                            localStorage.removeItem("user_id");
                            localStorage.removeItem("user_name");
                            sessionStorage.user_token = response.data.token;
                            sessionStorage.user_id = response.data.id;
                            sessionStorage.user_name = response.data.username;
                        }
                        // 登录成功页面跳转
                        let self = this;
                        this.$alert("登录成功！", "路飞学城", {
                            callback() {
                                self.$router.push("/");
                            }
                        });
                    }).catch(error => {
                        this.$message.error("对不起，登录失败！请确认密码或账号是否正确！");
                    });
                }
            },
            get_geetest_captcha() {
                // 获取验证码
                this.$axios.get(`${this.$settings.HOST}/user/captcha/`, {
                    // get请求携带参数的方式
                    params: {
                        username: this.username,
                    }
                }).then(response => {
                    // 使用initGeetest接口
                    // 参数1: 配置参数
                    // 参数2: 回调, 回调的第一个参数验证码对象,之后可以使用它做appentTo之类的事件
                    let data = JSON.parse(response.data); // 因为拿到的是字符串进行序列化处理
                    initGeetest({
                        gt: data.gt,
                        challenge: data.challenge,
                        product: "popup", // 产品形式，包括：float，embed，popup。注意只对PC版验证码有效
                        offline: !data.success // 表示用户后台检测极验服务器是否宕机，一般不需要关注
                    }, this.handlerPopup);
                }).catch(error => {
                    console.log(error.response);
                });
            },
            handlerPopup(captchaObj) {
                let self = this;   // 定义局部变量
                // 极验验证密码的验证方法
                captchaObj.onSuccess(function () {
                    var validate = captchaObj.getValidate();
                    // 当用户拖动验证码正确以后,发送请求给后端
                    self.$axios.post(`${self.$settings.HOST}/user/captcha/`, {
                        geetest_challenge: validate.geetest_challenge,
                        geetest_validate: validate.geetest_validate,
                        geetest_seccode: validate.geetest_seccode
                    }).then(response => {
                        if (response.data.status) {
                            // 验证码通过以后,才发送账号和密码进行登录!
                            self.loginHander(); // 调用上面的登录方法

                        }
                    }).catch(error => {
                        console.log(error.response);
                    });
                });
                //  加载验证码图片到前端
                document.getElementById('geetest1').innerHTML = "";
                // 将验证码加到id 为captcha的元素里
                captchaObj.appendTo("#geetest1");
            },
            smsHander() {
                this.mobile = this.username;
                // 发送短信
                // 1. 检查手机格式
                if (!/1[3-9]\d{9}/.test(this.mobile)) { // test检验手机格式
                    this.$message.error("手机号码格式不正确");
                    return false;
                }
                // 2.判断手机号码是否60s内接收过短信
                if (this.is_send_sms) {
                    this.$message.error("当前手机号已经在60秒内发送过短信,请不要频繁发送!");
                    return false;
                }
                // 3.发送ajax
                this.$axios.get(`${this.$settings.HOST}/user/sms/${this.mobile}/`).then(response => {
                    console.log(555, response.data);
                    this.is_send_sms = true;
                    let interval_time = 60;
                    let timer = setInterval(() => {
                        if (interval_time <= 1) {
                            // 停止倒计时
                            clearInterval(timer);
                            this.is_send_sms = false; //设置false,倒计时结束
                            this.sms_text = "获取验证码";
                        } else {
                            interval_time--;
                            this.sms_text = `${interval_time}秒后重新点击获取`;
                        }
                    }, 1000)
                }).catch(error => {
                    console.log(111, error.response);
                    console.log(222, error.response.data);
                    console.log(333, error.response.data.message);
                });
            },
            change_password() {
                // 发送ajax请求
                this.$axios.get(`${this.$settings.HOST}/user/email/`, {
                    params: {
                        username: this.username,
                    }
                }).then(response => {
                    console.log(response.data)
                }).catch(error => {
                    console.log(error.response)
                })
            }
        },

    };
</script>

<style scoped>
  .box {
    width: 100%;
    height: 100%;
    position: relative;
    overflow: hidden;
  }

  .box img {
    width: 100%;
    min-height: 100%;
  }

  .box .login {
    position: absolute;
    width: 500px;
    height: 400px;
    top: 0;
    left: 0;
    margin: auto;
    right: 0;
    bottom: 0;
    top: -338px;
  }

  .login .login-title {
    width: 100%;
    text-align: center;
  }

  .login-title img {
    width: 190px;
    height: auto;
  }

  .login-title p {
    font-family: PingFangSC-Regular;
    font-size: 18px;
    color: #fff;
    letter-spacing: .29px;
    padding-top: 10px;
    padding-bottom: 50px;
  }

  .login_box {
    width: 400px;
    height: auto;
    background: #fff;
    box-shadow: 0 2px 4px 0 rgba(0, 0, 0, .5);
    border-radius: 4px;
    margin: 0 auto;
    padding-bottom: 40px;
  }

  .login_box .title {
    font-size: 20px;
    color: #9b9b9b;
    letter-spacing: .32px;
    border-bottom: 1px solid #e6e6e6;
    display: flex;
    justify-content: space-around;
    padding: 50px 60px 0 60px;
    margin-bottom: 20px;
    cursor: pointer;
  }

  .login_box .title span:nth-of-type(1) {
    color: #4a4a4a;
    border-bottom: 2px solid #84cc39;
  }

  .inp {
    width: 350px;
    margin: 0 auto;
  }

  .inp input {
    border: 0;
    outline: 0;
    width: 100%;
    height: 45px;
    border-radius: 4px;
    border: 1px solid #d9d9d9;
    text-indent: 20px;
    font-size: 14px;
    background: #fff !important;
  }

  .inp input.user {
    margin-bottom: 16px;
  }

  .inp .rember {
    display: flex;
    justify-content: space-between;
    align-items: center;
    position: relative;
    margin-top: 10px;
  }

  .inp .rember p:first-of-type {
    font-size: 12px;
    color: #4a4a4a;
    letter-spacing: .19px;
    margin-left: 22px;
    display: -ms-flexbox;
    display: flex;
    -ms-flex-align: center;
    align-items: center;
    /*position: relative;*/
  }

  .inp .rember p:nth-of-type(2) {
    font-size: 14px;
    color: #9b9b9b;
    letter-spacing: .19px;
    cursor: pointer;
  }

  .inp .rember input {
    outline: 0;
    width: 30px;
    height: 45px;
    border-radius: 4px;
    border: 1px solid #d9d9d9;
    text-indent: 20px;
    font-size: 14px;
    background: #fff !important;
  }

  .inp .rember p span {
    display: inline-block;
    font-size: 12px;
    width: 100px;
    /*position: absolute;*/
    /*left: 20px;*/

  }

  #geetest {
    margin-top: 20px;
  }

  .login_btn {
    width: 100%;
    height: 45px;
    background: #84cc39;
    border-radius: 5px;
    font-size: 16px;
    color: #fff;
    letter-spacing: .26px;
    margin-top: 30px;
  }

  .inp .go_login {
    text-align: center;
    font-size: 14px;
    color: #9b9b9b;
    letter-spacing: .26px;
    padding-top: 20px;
  }

  .inp .go_login span {
    color: #84cc39;
    cursor: pointer;
  }

  .sms-box {
    position: relative;
  }

  .sms-box .sms-btn {
    position: absolute;
    font-size: 14px;
    letter-spacing: 0.26px;
    top: 10px;
    right: 16px;
    border-left: 1px solid #484848;
    padding-left: 16px;
    padding-bottom: 4px;
    cursor: pointer;
    background: #ffffff;
  }
</style>
